package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.CatalogRebateDto;
import com.shop2cn.iapi.agentproducts.parameter.EditProductRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.service.EditProductRebateCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebate;
import com.ymttest.database.model.agentrebate.Ymt_AgentRebateExample;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
/**
*商品分佣设置Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_EditProductRebate{
	private static EditProductRebateBean editproductrebateBean;
	private static EditProductRebateCall editproductrebateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品分佣设置Test");
	}

	@Before
	public void caseUp() {
		editproductrebateBean=new EditProductRebateBean();
		editproductrebateCall=new EditProductRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(editproductrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditProductRebate_001() {
		Logger.start(true,"正确数据，成功");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("200", editproductrebateCall.getString("code"), "验证code");
			editproductrebateBean.getCatalogRebateList().forEach(x->{
				Ymt_AgentRebateExample example=new Ymt_AgentRebateExample();
				example.createCriteria().andCatalogIdEqualTo(x.getCatalogId());
				List<Ymt_AgentRebate> agnetrebates = SqlMapperFactory.getIntergratedProductMapper().selectYmtAgentRebateExample(example);
				System.out.println(FormatUtil.beanToGSONString(agnetrebates));
				Logger.verifyEquals(x.getAgentTypeId(), agnetrebates.get(0).getAgentTypeId(), "验证getAgentTypeId");
				Logger.verifyEquals(x.getCatalogId(), agnetrebates.get(0).getCatalogId(), "验证getCatalogId");
				Logger.verifyEquals(x.getRebateValue().setScale(2), agnetrebates.get(0).getRebateValue().setScale(2), "验证getRebateValue");
				Logger.verifyEquals(editproductrebateBean.getRebateType(), agnetrebates.get(0).getRebateType(), "验证getRebateType");
			});
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditProductRebate_002() {
		Logger.start(true,"agentTypeId为空，编辑失败");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateBean.getCatalogRebateList().get(0).setAgentTypeId(null);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("201", editproductrebateCall.getString("code"), "验证code");
			Logger.verifyEquals("代理类型错误！", editproductrebateCall.getString("message"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	//验证发送总线消息 commonService.syncMongoAction(req.getProductId(), req.getUserId(), UpdateAgentProductRebate);
	//
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditProductRebate_003() {
		Logger.start(true,"验证发送总线消息");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("200", editproductrebateCall.getString("code"), "验证code");
			verifyMongoCurMq(productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	//需要补这3种情况 SelfChooseProductPrice(0, "差价"),    AutomaticInheritScalePrice(1, "分佣比例"),    AutomaticInheritFixPrice(2, "固定分佣"); --目前先实现分佣比例其他2个没有实际业务可以暂时延后
	//分佣金额不能大于商品利润 SalePrice售价- buyingPrice 进货价 =利润    SalePrice售价*AutomaticInheritScalePrice 分佣比例=分佣金额 分佣金额不可以高于利润
	//没有设置佣金比例字段rebateValue失败
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_EditProductRebate_004() {
		Logger.start(true,"差价分佣");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateBean.setRebateType(0);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("201", editproductrebateCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_EditProductRebate_005() {
		Logger.start(true,"固定分佣");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateBean.setRebateType(2);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("201", editproductrebateCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EditProductRebate_006() {
		Logger.start(true,"没有设置比例，失败");
		try {
			int userId = 500945639;
			int AgentUserId = 500945640;
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			editproductrebateBean = AddAgentProductCallService.initEditProductRebateBean(productId, AgentUserId,userId,20);
			editproductrebateBean.getCatalogRebateList().get(0).setRebateValue(null);
			editproductrebateCall.setData(editproductrebateBean);
			editproductrebateCall.callService();
			Logger.verifyEquals("500", editproductrebateCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public void verifyMongoCurMq(String pid) throws ClientProtocolException, IOException, InterruptedException {
//		验证发送总线消息 commonService.syncMongoAction(req.getProductId(), req.getUserId(), UpdateAgentProductRebate);
		Thread.sleep(5000);
		MessageListBean MessageListBean2 = MqQueryCallService.initEditProductRebate(pid);
		 
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug(joa2);
		int redo = 0;
		while (joa2.size() < 2 && redo < 3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
		String actionType = "UpdateAgentProductRebate";
		String appId = "agentproducts.iapi.ymatou.com";
		Logger.verifyEquals(actionType, joa2.get(0).get("ActionType"), "验证ActionType");
		Logger.verifyEquals(pid, joa2.get(0).get("ProductId"), "验证ProductId");
		Logger.verifyEquals(appId, joa2.get(0).get("AppId"), "验证AppId");
	}
	
	
}